package com.hxc.interest.service.impl;

import java.util.List;

import com.hxc.common.core.domain.AjaxResult;
import com.hxc.common.utils.DateUtils;
import com.hxc.common.utils.DistanceUtils;
import com.hxc.enums.BackTypes;
import com.hxc.enums.ReactionStateTypes;
import com.hxc.enums.SignInTypes;
import com.hxc.interest.domain.TReactionInfo;
import com.hxc.interest.mapper.TReactionInfoMapper;
import com.hxc.interest.vo.SigninParam;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.hxc.interest.mapper.TReactionBackMapper;
import com.hxc.interest.domain.TReactionBack;
import com.hxc.interest.service.ITReactionBackService;

/**
 * 政务反馈信息Service业务层处理
 * 
 * @author hxc
 * @date 2021-08-18
 */
@Service
public class TReactionBackServiceImpl implements ITReactionBackService 
{
    @Autowired
    private TReactionBackMapper tReactionBackMapper;

    @Autowired
    private TReactionInfoMapper tReactionInfoMapper;

    @Value("${config.space-num}")
    private Integer spaceNum;

    /**
     * 查询政务反馈信息
     * 
     * @param id 政务反馈信息主键
     * @return 政务反馈信息
     */
    @Override
    public TReactionBack selectTReactionBackById(Long id)
    {
        TReactionBack reactionBack = tReactionBackMapper.selectTReactionBackById(id);
        if(ObjectUtils.isNotEmpty(reactionBack)){
            //判断上传图片是否为null
            if(StringUtils.isNotBlank(reactionBack.getImgUrl())){
                reactionBack.setImgList(reactionBack.getImgUrl().split(","));
            }
        }
        return reactionBack;
    }

    /**
     * 查询政务反馈信息列表
     * 
     * @param tReactionBack 政务反馈信息
     * @return 政务反馈信息
     */
    @Override
    public List<TReactionBack> selectTReactionBackList(TReactionBack tReactionBack)
    {
        List<TReactionBack> reactionBackList = tReactionBackMapper.selectTReactionBackList(tReactionBack);
        if(reactionBackList.size() > 0){
            reactionBackList.forEach(reactionBack -> {
                //判断上传图片是否为 null
                if(StringUtils.isNotBlank(reactionBack.getImgUrl())){
                    reactionBack.setImgList(reactionBack.getImgUrl().split(","));
                }
            });
        }
        return reactionBackList;
    }

    /**
     * 新增政务反馈信息
     * 
     * @param tReactionBack 政务反馈信息
     * @return 结果
     */
    @Override
    public int insertTReactionBack(TReactionBack tReactionBack)
    {
        //修改反馈事件状态（已解决）
        if (StringUtils.equals(BackTypes.PROCESSING.getCode(),tReactionBack.getBackStauts())){
            updateReactionInfoState(tReactionBack);
        }
        tReactionBack.setCreateTime(DateUtils.getNowDate());
        if (StringUtils.isBlank(tReactionBack.getSignIn().toString())){
            tReactionBack.setSignIn(SignInTypes.WQD.getCode());
        }
        return tReactionBackMapper.insertTReactionBack(tReactionBack);
    }

    //修改反馈事件状态
    public void updateReactionInfoState(TReactionBack tReactionBack){
        TReactionInfo reactionInfo = new TReactionInfo();
        reactionInfo.setId(tReactionBack.getReactionId());
        reactionInfo.setReactionState(ReactionStateTypes.YGJ.getCode());
        tReactionInfoMapper.updateTReactionInfo(reactionInfo);
    }

    /**
     * 修改政务反馈信息
     * 
     * @param tReactionBack 政务反馈信息
     * @return 结果
     */
    @Override
    public int updateTReactionBack(TReactionBack tReactionBack)
    {
        //修改反馈事件状态（已解决）
        if (StringUtils.equals(BackTypes.PROCESSING.getCode(),tReactionBack.getBackStauts())){
            updateReactionInfoState(tReactionBack);
        }
        return tReactionBackMapper.updateTReactionBack(tReactionBack);
    }

    /**
     * 批量删除政务反馈信息
     * 
     * @param ids 需要删除的政务反馈信息主键
     * @return 结果
     */
    @Override
    public int deleteTReactionBackByIds(Long[] ids)
    {
        return tReactionBackMapper.deleteTReactionBackByIds(ids);
    }

    /**
     * 删除政务反馈信息信息
     * 
     * @param id 政务反馈信息主键
     * @return 结果
     */
    @Override
    public int deleteTReactionBackById(Long id)
    {
        return tReactionBackMapper.deleteTReactionBackById(id);
    }

    /**
     * 功能描述:政务事件签到
     * @author yzj
     * @param
     */
    @Override
    public AjaxResult querySignin(SigninParam param) {
        TReactionInfo reactionInfo = tReactionInfoMapper.selectTReactionInfoById(param.getReactionId());
        if (ObjectUtils.isNotEmpty(reactionInfo)) {
            Boolean space = DistanceUtils.querySpace(param.getLongitude(), param.getLongitude(),
                    reactionInfo.getLongitude(), reactionInfo.getLatitude(), spaceNum);
            if (space) {
                return AjaxResult.success("success");
            } else {
                return AjaxResult.error("不再目标坐标范围");
            }
        } else {
            return AjaxResult.error("查询事件信息不存在");
        }
    }

}
